package pers.cocoadel.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import pers.cocoadel.springcloud.entities.CommonResult;
import pers.cocoadel.springcloud.entities.Payment;
import pers.cocoadel.springcloud.service.PaymentService;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author rwby
 */
@Slf4j
@RestController()
@RequestMapping("/payment")
public class PaymentController
{

    @Value("${server.port}")
    private String servicePort;

    private final DiscoveryClient discoveryClient;

    private final PaymentService paymentService;

    @Autowired(required = false)
    public PaymentController(DiscoveryClient discoveryClient, PaymentService paymentService)
    {
        this.discoveryClient = discoveryClient;
        this.paymentService = paymentService;
    }

    @GetMapping("/get/{id}")
    public CommonResult<Payment> getById(@PathVariable("id") Long id)
    {
        Payment payment = paymentService.getById(id);
        log.info("查询结果：" + payment);
        if (payment == null)
        {
            return new CommonResult<>(444,
                    String.format("查询结果失败[servicePort:%s]，查询id：%s", servicePort, id), null);
        }
        else
        {
            return new CommonResult<>(200, String.format("查询成功[servicePort:%s]", servicePort), payment);
        }
    }

    @PostMapping("/create")
    public CommonResult<Payment> create(@RequestBody Payment payment)
    {
        int result = paymentService.create(payment);
        log.info("插入数据结果:" + result);
        if (result > 0)
        {
            return new CommonResult<>(200, "插入数据成功", payment);
        }
        else
        {
            return new CommonResult<>(444, "插入数据失败！", null);
        }
    }


    @GetMapping("/payment/discovery")
    public Object discovery()
    {
        List<String> services = discoveryClient.getServices();
        for (String service : services)
        {
            log.info("service:" + service);
        }

        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("cloud-payment-service");
        for (ServiceInstance instance : serviceInstances)
        {
            log.info(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
        }
        return services;
    }

    @GetMapping("/lb")
    public String getPaymentLb()
    {
        return servicePort;
    }


    @GetMapping("/feign/time_out")
    public String getPaymentTimeOut()
    {
        try
        {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        return servicePort + " o(∩_∩)o 哈哈";
    }


}
